<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsTemplateConfig:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ configId: record.configId })">
          {{ record.adaptGrade }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <TempLate @register="tempregiser" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsTemplateConfigList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    sportsTemplateConfigDelete,
    sportsTemplateConfigListData,
    sportsTemplateConfigStatus,
  } from '/@/api/sports/sportsTemplateConfig';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import TempLate from './tempLate.vue';

  const { t } = useI18n('sports.sportsTemplateConfig');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('体测模板配置管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('适配年级'),
        field: 'adaptGrade',
        component: 'Select',
        componentProps: {
          dictType: 'vke_grade',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a.adapt_grade',
      //sorter: true,
      width: 230,
      align: 'left',
      dictType: 'vke_grade',
    },
    {
      title: t('适配性别'),
      dataIndex: 'adaptSex',
      key: 'a.adapt_sex',
      //sorter: true,
      width: 200,
      align: 'left',
      dictType: 'sports_user_sex',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'status',
      dictType: 'sys_status',
      width: 200,
      align: 'left',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      //sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑体测模板'),
        onClick: handleForm.bind(this, {
          configId: record.configId,
          adaptGrade: record.adaptGrade,
        }),
        auth: 'sports:sportsTemplateConfig:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用用户'),
        popConfirm: {
          title: t('是否确认停用用户'),
          confirm: handleDisable.bind(this, { configId: record.configId, status: '2' }),
        },
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用用户'),
        popConfirm: {
          title: t('是否确认启用用户'),
          confirm: handleEnable.bind(this, { configId: record.configId, status: '0' }),
        },
        auth: 'sys:empUser:updateStatus',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:book-outlined',
        title: t('预览模板'),
        //label: t('预览模板'),
        onClick: preview.bind(this, record),
        auth: 'sports:sportsTemplateConfig:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除体测模板'),
        popConfirm: {
          title: t('是否确认删除体测模板'),
          confirm: handleDelete.bind(this, { configId: record.configId }),
        },
        auth: 'sports:sportsTemplateConfig:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: sportsTemplateConfigListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [tempregiser, { openDrawer: openTempLate }] = useDrawer();
  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await sportsTemplateConfigStatus(record);
    showMessage(res.message);
    handleSuccess();
  }
  async function handleEnable(record: Recordable) {
    const res = await sportsTemplateConfigStatus(record);
    showMessage(res.message);
    handleSuccess();
  }
  function preview(record: Recordable) {
    openTempLate(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsTemplateConfigDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
